<template>
	<view class="container">
		<!-- 自定义导航栏 -->
		<view class="nav-bar" :style="{ paddingTop: safeAreaInsets.top + 'px' }">
			<view class="nav-left" @click="goBack">
				<uni-icons type="left" size="24"></uni-icons>
			</view>
			<view class="nav-title">{{ categoryName }}</view>
			<view class="nav-right"></view>
		</view>

		<!-- 商品列表 -->
		<scroll-view class="goods-list" :style="{ height: scrollViewHeight }" scroll-y>
			<view class="goods-item" v-for="(item, index) in goodsList" :key="index" @click="gotoshop(item)">
				<image class="goods-image" :src="item.img" mode="aspectFill"></image>
				<view class="goods-info">
					<text class="goods-name">{{ item.name }}</text>
					<text class="goods-price">￥{{ item.price }}</text>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				categoryId: '',
				categoryName: '商品列表',
				goodsList: [],
				safeAreaInsets: {
					top: 0,
					bottom: 0
				}
			};
		},
		computed: {
			scrollViewHeight() {
				return `calc(100vh - ${this.safeAreaInsets.top + 90}px)`
			}
		},
		onLoad(options) {
			this.categoryId = options.categoryId;
			this.getCategoryName();
			this.fetchGoodsList();
			this.getSafeAreaInsets();
		},
		methods: {
			goBack() {
				uni.navigateBack();
			},
			// 获取分类名称
			getCategoryName() {
				uni.request({
					url: 'http://localhost:8080/goodscategory/goodscategory/list',
					header: {
						'Content-Type': 'application/json',
						'Authorization': uni.getStorageSync('token'),
					},
					success: res => {
						if (res.data.code === 200) {
							const category = res.data.rows.find(
								item => item.categoryId == this.categoryId
							);
							if (category) {
								this.categoryName = category.categoryName;
							}
						}
					}
				});
			},
			// 获取商品列表
			fetchGoodsList() {
				uni.request({
					url: `http://localhost:8080/goods/goods/list?categoryId=${this.categoryId}`,
					header: {
						'Content-Type': 'application/json',
						'Authorization': uni.getStorageSync('token'),
					},
					success: res => {
						if (res.data.code === 200) {
							this.goodsList = res.data.rows.map(item => ({
								name: item.goodsName,
								price: item.goodsPrice,
								goodsId: item.goodsId,
								img: `http://localhost:8080${item.goodsImage1}`
							}));
						}
					}
				});
			},
			// 获取安全区域信息
			getSafeAreaInsets() {
				uni.getSystemInfo({
					success: (res) => {
						this.safeAreaInsets = {
							top: res.safeArea ? res.safeArea.top : res.statusBarHeight,
							bottom: res.safeArea ? res.safeArea.bottom : 0
						};
					},
					fail: (err) => {
						console.error('获取系统信息失败:', err);
					}
				});
			},
			gotoshop(item) {
				console.log('跳转传递的 goodsId:', item.goodsId); // 调试日志
				uni.navigateTo({
					url: `/pages/shop-details/shop-details?goodsId=${item.goodsId}`
				});
			},
		}
	};
</script>

<style scoped>
	.container {
		background-color: #FFF8F0;
		min-height: 100vh;
	}

	.nav-bar {
		display: flex;
		align-items: center;
		height: 90rpx;
		padding: 0 30rpx;
		background-color: #fff;
		border-bottom: 1rpx solid #eee;
		position: sticky;
		top: 0;
		z-index: 100;
	}

	.nav-left {
		width: 60rpx;
	}

	.nav-title {
		flex: 1;
		text-align: center;
		font-size: 36rpx;
		font-weight: bold;
	}

	.goods-list {
		width: 96%;
		margin: 10px auto 0;
	}

	.goods-item {
		display: flex;
		background-color: #fff;
		margin-bottom: 20rpx;
		padding: 20rpx;
		border-radius: 10rpx;
		box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
	}

	.goods-image {
		/* 	width: 200rpx;
		height: 200rpx; */
		width: 80px;
		height: 80px;
		border-radius: 10rpx;
		margin-right: 20rpx;
	}

	.goods-info {
		/* 	flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-around; */
		/* justify-content: space-between; */
	}

	.goods-name {
		/* 		font-size: 30rpx;
		color: #333;
		line-height: 1.4; */

		color: #333;
		font-size: 30rpx;
		display: block;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		margin-bottom: 5px;
		flex: 1;

		margin-top: 10px;
		margin-bottom: 10px;
	}

	.goods-price {
		font-size: 32rpx;
		color: #e4393c;
		font-weight: bold;
	}
</style>
